package com.molichuxing.gateway.bff.payment.convertor;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.molichuxing.framework.property.OperatorTypeEnum;
import com.molichuxing.framework.utils.BigDecimalUtils;
import com.molichuxing.framework.utils.DateUtil;
import com.molichuxing.gateway.bff.payment.entity.request.create.OrderPayCreateVo;
import com.molichuxing.gateway.bff.payment.entity.request.modify.AioPayResultModifyVo;
import com.molichuxing.gateway.bff.payment.entity.response.CashierDeskVo;
import com.molichuxing.gateway.bff.payment.entity.response.PaymentRecordVo;
import com.molichuxing.gateway.contants.OrderConst;
import com.molichuxing.services.business.dto.request.create.PaymentBizCreateDto;
import com.molichuxing.services.business.dto.request.create.PaymentDetailBizCreateDto;
import com.molichuxing.services.business.dto.request.modify.AioPayResultBizModifyDto;
import com.molichuxing.services.business.dto.request.modify.OrderPayModifyBizDto;
import com.molichuxing.services.business.dto.response.NotifyBizDto;
import com.molichuxing.services.business.dto.response.OrderExclusivelBizDto;
import com.molichuxing.services.business.dto.response.OrderMileageBizDto;
import com.molichuxing.services.business.dto.response.OrderSubscribeBizDto;
import com.molichuxing.services.business.dto.response.PaymentRecordBizDto;
import com.molichuxing.services.infrastructure.dto.request.create.InvoiceCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.OrderPayModifyDto;
import com.molichuxing.services.infrastructure.dto.response.DealerDetailDto;
import com.molichuxing.services.infrastructure.dto.response.OrderExclusivelDto;
import com.molichuxing.services.infrastructure.dto.response.OrderRenegeDto;
import com.molichuxing.services.infrastructure.dto.response.OrderScheduleDto;
import com.molichuxing.services.infrastructure.dto.response.OrderSubscribeDto;
import com.molichuxing.services.infrastructure.dto.response.OrderSubscribeSettlementDto;
import com.molichuxing.services.infrastructure.dto.response.UserDto;
import com.molichuxing.services.property.InvoiceBusinessTypeEnum;
import com.molichuxing.services.property.InvoiceTypeEnum;
import com.molichuxing.services.property.OrderPayWayEnum;
import com.molichuxing.services.property.OrderPaymentMethodEnum;
import com.molichuxing.services.property.PaymentBusinessTypeEnum;
import com.molichuxing.services.property.PaymentCostTypeEnum;
import com.molichuxing.services.property.PaymentDetailTypeEnum;
import com.molichuxing.services.property.PaymentPayWayEnum;

public class Convert {

	public static PaymentRecordVo toPaymentRecordVo(PaymentRecordBizDto beanBizDto, DecimalFormat df) {
		PaymentRecordVo beanVo = null;
		if (beanBizDto != null) {
			beanVo = new PaymentRecordVo();
			beanVo.setPaymentAmount(beanBizDto.getPayAmount());
			// beanVo.setPaymentAmountStr("¥" +
			// df.format(beanBizDto.getPayAmount()));
			beanVo.setPaymentAmountStr("¥" + BigDecimalUtils.toString(beanBizDto.getPayAmount()));
		}

		return beanVo;
	}

	public static CashierDeskVo toCashierDeskVo(OrderExclusivelBizDto beanBizDto) {
		CashierDeskVo beanVo = null;
		if (beanBizDto != null) {
			beanVo = new CashierDeskVo();
			// 首付金额（租车费+保证金）
			beanVo.setPayAmount(beanBizDto.getRental().add(beanBizDto.getDeposit()));
			// 订单号
			beanVo.setOrderCode(beanBizDto.getOrderCode());
			// 服务器当前时间
			beanVo.setCurrentDate(LocalDateTime.now());
			// 订单过期时间
			beanVo.setExpiryDate(
					DateUtil.plus(beanBizDto.getOrderTime(), OrderConst.ORDER_VALID_TIME.longValue(),
							ChronoUnit.MINUTES));
		}
		return beanVo;
	}

	public static CashierDeskVo toCashierDeskVo(OrderSubscribeBizDto beanBizDto) {
		CashierDeskVo beanVo = null;
		if (beanBizDto != null) {
			beanVo = new CashierDeskVo();
			// 首付金额（租车费+保证金）
			beanVo.setPayAmount(beanBizDto.getCartFee().add(beanBizDto.getDeposit()));
			// 订单号
			beanVo.setOrderCode(beanBizDto.getOrderCode());
			// 服务器当前时间
			beanVo.setCurrentDate(LocalDateTime.now());
			// 订单过期时间
			beanVo.setExpiryDate(DateUtil.plus(beanBizDto.getOrderTime(), OrderConst.ORDER_VALID_TIME.longValue(),
					ChronoUnit.MINUTES));
		}
		return beanVo;
	}

	public static CashierDeskVo toCashierDeskVo(OrderSubscribeSettlementDto beanDto) {
		CashierDeskVo beanVo = null;
		if (beanDto != null) {
			beanVo = new CashierDeskVo();
			// 支付金额
			beanVo.setPayAmount(beanDto.getTotalFee());
			// 订单号
			beanVo.setOrderCode(beanDto.getOrderCode());
			// 服务器当前时间
			beanVo.setCurrentDate(LocalDateTime.now());
		}
		return beanVo;
	}

	public static CashierDeskVo toCashierDeskVo(OrderScheduleDto beanDto) {
		CashierDeskVo beanVo = null;
		if (beanDto != null) {
			beanVo = new CashierDeskVo();
			// 支付金额
			beanVo.setPayAmount(beanDto.getPayable());
			// 订单号
			beanVo.setOrderCode(beanDto.getOrderCode());
			// 排期id
			beanVo.setScheduleId(beanDto.getId());
			// 服务器当前时间
			beanVo.setCurrentDate(LocalDateTime.now());
		}
		return beanVo;
	}

	public static CashierDeskVo toCashierDeskVo(OrderRenegeDto beanDto) {
		CashierDeskVo beanVo = null;
		if (beanDto != null) {
			beanVo = new CashierDeskVo();
			// 支付金额
			beanVo.setPayAmount(beanDto.getCostAmount());
			// 订单号
			beanVo.setOrderCode(beanDto.getOrderCode());
			// 服务器当前时间
			beanVo.setCurrentDate(LocalDateTime.now());
		}
		return beanVo;
	}

	public static CashierDeskVo toCashierDeskVo(OrderMileageBizDto beanBizDto) {
		CashierDeskVo beanVo = null;
		if (beanBizDto != null) {
			beanVo = new CashierDeskVo();
			// 支付金额
			beanVo.setPayAmount(beanBizDto.getPurchaseFee().add(beanBizDto.getBeyondFee()));
			// 订单号
			beanVo.setOrderCode(beanBizDto.getOrderCode());
			// 排期id
			beanVo.setScheduleId(beanBizDto.getId());
			// 服务器当前时间
			beanVo.setCurrentDate(LocalDateTime.now());
			// 订单过期时间
			beanVo.setExpiryDate(DateUtil.plus(beanBizDto.getPurchaseTime(), OrderConst.PURCHASE_VALID_TIME.longValue(),
					ChronoUnit.MINUTES));
		}
		return beanVo;
	}

	public static PaymentBizCreateDto toPaymentBizCreateDto(OrderExclusivelBizDto orderBizDto,
			OrderPayCreateVo createVo, BigDecimal payAmount, Integer scheduleId, Integer periods, Integer totalPeriods,
			PaymentCostTypeEnum costType) {

		PaymentBizCreateDto beanBizDto = new PaymentBizCreateDto();
		beanBizDto.setOrderCode(orderBizDto.getOrderCode());
		beanBizDto.setUserId(createVo.getOperateId());
		beanBizDto.setDeviceInfo(createVo.getIp());
		beanBizDto.setPayWay(createVo.getPayWay() == null ? null : PaymentPayWayEnum.getEnum(createVo.getPayWay()));
		beanBizDto.setPayAmount(payAmount);
		beanBizDto.setBusinessType(PaymentBusinessTypeEnum.EXCLUSIVEL);
		beanBizDto.setCostType(costType);
		beanBizDto.setOpenid(createVo.getOpenid());
		beanBizDto.setScheduleId(scheduleId);
		if (periods == 1) {
			// 首付
			beanBizDto.setTimeStart(orderBizDto.getOrderTime());
			beanBizDto.setTimeEnd(DateUtil.plus(orderBizDto.getOrderTime(), OrderConst.ORDER_VALID_TIME.longValue(),
					ChronoUnit.MINUTES));
			beanBizDto.setRemark(OrderPaymentMethodEnum.FULL.equals(orderBizDto.getPaymentMethod()) ? "全款，租车费和保证金"
					: totalPeriods + "期，首次租车费和保证金");
			// 支付详情
			beanBizDto
					.setDetails(
							Arrays.asList(new PaymentDetailBizCreateDto(
									OrderPaymentMethodEnum.FULL.equals(orderBizDto.getPaymentMethod())
											? orderBizDto.getCartFee() : orderBizDto.getRental(),
									PaymentDetailTypeEnum.CART_FEE),
									new PaymentDetailBizCreateDto(orderBizDto.getDeposit(),
											PaymentDetailTypeEnum.DEPOSIT)));
		} else {
			// 还款、退租缴费
			beanBizDto.setTimeStart(LocalDateTime.now());
			if (PaymentPayWayEnum.WXPAY_MWEB.equals(beanBizDto.getPayWay())) {
				// H5支付
				beanBizDto.setTimeEnd(DateUtil.plus(LocalDateTime.now(), 5L, ChronoUnit.MINUTES));
			} else {
				beanBizDto.setTimeEnd(
						DateUtil.plus(LocalDateTime.now(), OrderConst.PAY_VALID_TIME.longValue(), ChronoUnit.MINUTES));
			}

			if (PaymentCostTypeEnum.REPAYMENT.equals(costType)) {
				// 还款
				beanBizDto.setRemark(totalPeriods + "期，第" + periods + "期 租车费");
				// 支付详情
				beanBizDto.setDetails(
						Arrays.asList(new PaymentDetailBizCreateDto(payAmount, PaymentDetailTypeEnum.CART_FEE)));
			} else if (PaymentCostTypeEnum.RENEGE.equals(costType)) {
				// 退租缴费
				beanBizDto.setRemark("退租违约金");
				// 支付详情
				beanBizDto.setDetails(
						Arrays.asList(new PaymentDetailBizCreateDto(payAmount, PaymentDetailTypeEnum.RENEGE_FEE)));
			}

		}
		return beanBizDto;
	}

	public static PaymentBizCreateDto toPaymentBizCreateDto(OrderSubscribeBizDto orderBizDto, OrderPayCreateVo createVo,
			BigDecimal payAmount, PaymentCostTypeEnum costType, OrderSubscribeSettlementDto settlementDto) {
		PaymentBizCreateDto beanBizDto = new PaymentBizCreateDto();
		beanBizDto.setOrderCode(orderBizDto.getOrderCode());
		beanBizDto.setUserId(createVo.getOperateId());
		beanBizDto.setDeviceInfo(createVo.getIp());
		beanBizDto.setPayWay(PaymentPayWayEnum.getEnum(createVo.getPayWay()));
		beanBizDto.setPayAmount(payAmount);
		beanBizDto.setBusinessType(PaymentBusinessTypeEnum.SUBSCRIBE);
		beanBizDto.setCostType(costType);
		beanBizDto.setOpenid(createVo.getOpenid());
		beanBizDto.setScheduleId(0);
		if (PaymentCostTypeEnum.SUBSCRIBE.equals(costType)) {
			// 订购
			beanBizDto.setTimeStart(orderBizDto.getOrderTime());
			beanBizDto.setTimeEnd(
					DateUtil.plus(orderBizDto.getOrderTime(), OrderConst.ORDER_VALID_TIME.longValue(),
							ChronoUnit.MINUTES));
			beanBizDto.setRemark("订阅里程");

			// 支付详情
			beanBizDto.setDetails(
					Arrays.asList(
							new PaymentDetailBizCreateDto(orderBizDto.getRental(), PaymentDetailTypeEnum.CART_FEE),
							new PaymentDetailBizCreateDto(orderBizDto.getBaseMileagePrice(),
									PaymentDetailTypeEnum.BASE_MILEAGE_FEE),
							new PaymentDetailBizCreateDto(orderBizDto.getPackageMileagePrice(),
									PaymentDetailTypeEnum.PACKAGE_MILEAGE_FEE),
							new PaymentDetailBizCreateDto(orderBizDto.getDeposit(), PaymentDetailTypeEnum.DEPOSIT)));

		} else if (PaymentCostTypeEnum.RENEW.equals(costType)) {
			// 续订
			beanBizDto.setTimeStart(orderBizDto.getOrderTime());
			beanBizDto.setTimeEnd(DateUtil.plus(orderBizDto.getOrderTime(), OrderConst.ORDER_VALID_TIME.longValue(),
					ChronoUnit.MINUTES));
			beanBizDto.setRemark("订阅里程-续订");
			// 支付详情
			beanBizDto.setDetails(Arrays.asList(
					new PaymentDetailBizCreateDto(orderBizDto.getRental(), PaymentDetailTypeEnum.CART_FEE),
					new PaymentDetailBizCreateDto(orderBizDto.getBaseMileagePrice(),
							PaymentDetailTypeEnum.BASE_MILEAGE_FEE),
					new PaymentDetailBizCreateDto(orderBizDto.getPackageMileagePrice(),
							PaymentDetailTypeEnum.PACKAGE_MILEAGE_FEE)));
		} else if (PaymentCostTypeEnum.SETTLEMENT.equals(costType)) {
			// 结算
			beanBizDto.setTimeStart(LocalDateTime.now());
			beanBizDto.setTimeEnd(
					DateUtil.plus(LocalDateTime.now(), OrderConst.PAY_VALID_TIME.longValue(), ChronoUnit.MINUTES));
			beanBizDto.setRemark("订阅里程-补缴费");
			// 支付详情
			List<PaymentDetailBizCreateDto> details = new ArrayList<>();
			if (settlementDto.getBeyondMileageFee() != null && settlementDto.getBeyondMileageFee().compareTo(BigDecimal.ZERO) > 0) {
				// 超里程费
				details.add(new PaymentDetailBizCreateDto(settlementDto.getBeyondMileageFee(), PaymentDetailTypeEnum.BEYOND_MILEAGE_FEE));
			}
			if (settlementDto.getBeyondDayFee() != null
					&& settlementDto.getBeyondDayFee().compareTo(BigDecimal.ZERO) > 0) {
				// 超时费
				details.add(new PaymentDetailBizCreateDto(settlementDto.getBeyondDayFee(),
						PaymentDetailTypeEnum.TIME_OUT_FEE));
			}
			if (settlementDto.getDamageFee() != null
					&& settlementDto.getDamageFee().compareTo(BigDecimal.ZERO) > 0) {
				// 质损费
				details.add(new PaymentDetailBizCreateDto(settlementDto.getDamageFee(),
						PaymentDetailTypeEnum.QUALITY_LOSS));
			}
			beanBizDto.setDetails(details);
		}
		return beanBizDto;
	}

	public static PaymentBizCreateDto toPaymentBizCreateDto(OrderMileageBizDto orderBizDto, OrderPayCreateVo createVo,
			BigDecimal payAmount, PaymentCostTypeEnum costType) {
		PaymentBizCreateDto beanBizDto = new PaymentBizCreateDto();
		beanBizDto.setOrderCode(orderBizDto.getOrderCode());
		beanBizDto.setUserId(createVo.getOperateId());
		beanBizDto.setDeviceInfo(createVo.getIp());
		beanBizDto.setPayWay(PaymentPayWayEnum.getEnum(createVo.getPayWay()));
		beanBizDto.setPayAmount(payAmount);
		beanBizDto.setBusinessType(PaymentBusinessTypeEnum.SUBSCRIBE);
		beanBizDto.setCostType(costType);
		beanBizDto.setOpenid(createVo.getOpenid());
		beanBizDto.setScheduleId(0);
		beanBizDto.setTimeStart(orderBizDto.getPurchaseTime());
		beanBizDto.setTimeEnd(DateUtil.plus(orderBizDto.getPurchaseTime(), OrderConst.PURCHASE_VALID_TIME.longValue(),
				ChronoUnit.MINUTES));
		beanBizDto.setRemark("订阅里程-加购");

		// 支付详情
		List<PaymentDetailBizCreateDto> details = new ArrayList<>();
		// 加购里程金额
		details.add(new PaymentDetailBizCreateDto(orderBizDto.getPurchaseFee(), PaymentDetailTypeEnum.ADD_MILEAGE_FEE));
		if (orderBizDto.getBeyondFee() != null && orderBizDto.getBeyondFee().compareTo(BigDecimal.ZERO) > 0) {
			details.add(new PaymentDetailBizCreateDto(orderBizDto.getBeyondFee(),
					PaymentDetailTypeEnum.BEYOND_MILEAGE_FEE));
		}
		beanBizDto.setDetails(details);
		return beanBizDto;
	}

	public static OrderPayModifyBizDto toOrderPayModifyBizDto(NotifyBizDto notifyBizDto, UserDto userDto) {
		OrderPayModifyBizDto modifyDto = new OrderPayModifyBizDto();
		modifyDto.setOperateId(userDto.getId());
		modifyDto.setOperateName(userDto.getRealName());
		modifyDto.setOperatorType(OperatorTypeEnum.MEMBER);
		modifyDto.setOrderCode(notifyBizDto.getOrderCode());
		modifyDto.setPayTime(notifyBizDto.getPayTime());
		modifyDto.setPayWay(Enum.valueOf(OrderPayWayEnum.class, notifyBizDto.getPayWay().toString()));

		return modifyDto;
	}

	public static OrderPayModifyDto toOrderPayModifyDto(NotifyBizDto notifyBizDto, UserDto userDto) {
		OrderPayModifyDto modifyDto = new OrderPayModifyDto();
		modifyDto.setOperateId(userDto.getId());
		modifyDto.setOperateName(userDto.getRealName());
		modifyDto.setOperatorType(OperatorTypeEnum.MEMBER);
		modifyDto.setOrderCode(notifyBizDto.getOrderCode());
		modifyDto.setPayTime(notifyBizDto.getPayTime());
		modifyDto.setPayWay(Enum.valueOf(OrderPayWayEnum.class, notifyBizDto.getPayWay().toString()));
		modifyDto.setScheduleId(notifyBizDto.getScheduleId());

		return modifyDto;
	}

	public static AioPayResultBizModifyDto toAioPayResultBizModifyDto(AioPayResultModifyVo modifyVo) {
		AioPayResultBizModifyDto modifyDto = null;
		if (modifyVo != null) {
			modifyDto = new AioPayResultBizModifyDto();
			modifyDto.setPayNumber(modifyVo.getPayNumber());
			modifyDto.setPayTime(modifyVo.getPayTime());
			modifyDto.setThirdPayNumber(modifyVo.getThirdPayNumber());
			modifyDto.setPayResultJson(modifyVo.getPayResultJson());
			modifyDto.setPayAmount(modifyVo.getPayAmount());
			modifyDto.setSign(modifyVo.getSign());
			modifyDto.setPayWay(PaymentPayWayEnum.getEnum(modifyVo.getPayWay()));
			modifyDto.setIsSuccess(modifyVo.getIsSuccess() == 0 ? false : true);
		}
		return modifyDto;
	}

	public static InvoiceCreateDto toInvoiceCreateDto(NotifyBizDto notify, OrderExclusivelDto orderExclusivel,
			OrderScheduleDto orderSchedule, DealerDetailDto dealerDetail) {
		InvoiceCreateDto createDto = new InvoiceCreateDto();
		// 服务费
		BigDecimal serviceFee = orderExclusivel.getServiceFee().divide(new BigDecimal(orderSchedule.getTotalPeriods()),
				2, RoundingMode.HALF_DOWN);
		if (orderSchedule.getTotalPeriods() > 1 && orderSchedule.getTotalPeriods() == orderSchedule.getPeriods()) {
			// 最后一期
			serviceFee = orderExclusivel.getServiceFee()
					.subtract(serviceFee.multiply(new BigDecimal(orderSchedule.getTotalPeriods() - 1)));
		}

		// 订单号
		createDto.setOrderCode(orderExclusivel.getOrderCode());
		// 下单时间
		createDto.setOrderTime(orderExclusivel.getOrderTime());
		// 支付时间/续订单生效的时
		createDto.setPayTime(notify.getPayTime());
		// 支付金额
		createDto.setAmount(orderSchedule.getPayable());
		// 服务费
		createDto.setServiceFee(serviceFee);
		// 业务类型（1：订阅里程，2：专属里程）
		createDto.setBusinessType(InvoiceBusinessTypeEnum.EXCLUSIVEL.getValue());
		// 发票类型（11：主订单，12：续订单，13：加购，14：超时，15：超程，16：质损,21：分期，22：全款）
		createDto.setType(OrderPaymentMethodEnum.INSTALLMENT.equals(orderExclusivel.getPaymentMethod())
				? InvoiceTypeEnum.INSTALLMENT.getValue() : InvoiceTypeEnum.FULL_PAYMENT.getValue());
		// 当前期数
		createDto.setCurrentPeriods(orderSchedule == null ? 1 : orderSchedule.getPeriods());
		// 总期数
		createDto.setTotalPeriods(
				orderSchedule == null ? orderExclusivel.getTransportTime() : orderSchedule.getTotalPeriods());
		// 下单门店
		createDto.setStoreId(orderExclusivel.getPlaceStoreId());
		// 用户id
		createDto.setUserId(orderExclusivel.getUserId());
		// 用车时长
		createDto.setTransportTime(orderExclusivel.getTransportTime());
		// 所属分子公司
		createDto.setSubcompanyId(dealerDetail.getSubcompanyId());

		return createDto;
	}

	public static List<InvoiceCreateDto> toInvoiceCreateDto(NotifyBizDto notify,
			List<OrderScheduleDto> orderScheduleDtos, OrderExclusivelDto orderExclusivel,
			DealerDetailDto dealerDetail) {
		List<InvoiceCreateDto> createDtos = new ArrayList<>();

		// 服务费
		BigDecimal serviceFee = orderExclusivel.getServiceFee()
				.divide(new BigDecimal(orderScheduleDtos.get(0).getTotalPeriods()), 2, RoundingMode.HALF_DOWN);
		BigDecimal tmpServiceFee = BigDecimal.ZERO;
		for (OrderScheduleDto orderScheduleDto : orderScheduleDtos) {
			InvoiceCreateDto createDto = new InvoiceCreateDto();
			// 订单号
			createDto.setOrderCode(orderExclusivel.getOrderCode());
			// 下单时间
			createDto.setOrderTime(orderExclusivel.getOrderTime());
			// 支付时间/续订单生效的时
			createDto.setPayTime(notify.getPayTime());
			// 支付金额
			createDto.setAmount(orderScheduleDto.getPayable());
			// 服务费
			tmpServiceFee = tmpServiceFee.add(serviceFee);
			createDto.setServiceFee(orderScheduleDto.getPeriods() == orderScheduleDto.getTotalPeriods()
					? orderExclusivel.getServiceFee().subtract(tmpServiceFee) : serviceFee);
			// 业务类型（1：订阅里程，2：专属里程）
			createDto.setBusinessType(InvoiceBusinessTypeEnum.EXCLUSIVEL.getValue());
			// 发票类型（11：主订单，12：续订单，13：加购，14：超时，15：超程，16：质损,21：分期，22：全款）
			createDto.setType(InvoiceTypeEnum.FULL_PAYMENT.getValue());
			// 当前期数
			createDto.setCurrentPeriods(orderScheduleDto.getPeriods());
			// 总期数
			createDto.setTotalPeriods(orderScheduleDto.getTotalPeriods());
			// 下单门店
			createDto.setStoreId(orderExclusivel.getPlaceStoreId());
			// 用户id
			createDto.setUserId(orderExclusivel.getUserId());
			// 用车时长
			createDto.setTransportTime(orderExclusivel.getTransportTime());
			// 所属分子公司
			createDto.setSubcompanyId(dealerDetail.getSubcompanyId());

			createDtos.add(createDto);
		}

		return createDtos;
	}

	public static InvoiceCreateDto toInvoiceCreateDto(NotifyBizDto notify, OrderSubscribeDto orderSubscribe,
			DealerDetailDto dealerDetail) {
		InvoiceCreateDto createDto = new InvoiceCreateDto();
		// 订单号
		createDto.setOrderCode(orderSubscribe.getOrderCode());
		// 下单时间
		createDto.setOrderTime(orderSubscribe.getOrderTime());
		// 支付时间/续订单生效的时
		createDto.setPayTime(orderSubscribe.getParentOrderCode() == null ? notify.getPayTime()
				: DateUtil.getDayStart(orderSubscribe.getLeaseInception()));
		// 支付金额
		createDto.setAmount(orderSubscribe.getCartFee());
		// 服务费(金额乘以费率)
		createDto.setServiceFee(orderSubscribe.getCartFee()
				.multiply(orderSubscribe.getServiceFeeRate().divide(new BigDecimal(100), 2, RoundingMode.HALF_DOWN)));
		// 业务类型（1：订阅里程，2：专属里程）
		createDto.setBusinessType(InvoiceBusinessTypeEnum.SUBSCRIBE.getValue());
		// 发票类型（11：主订单，12：续订单，13：加购，14：超时，15：超程，16：质损,21：分期，22：全款）
		createDto.setType(orderSubscribe.getParentOrderCode() == null ? InvoiceTypeEnum.MAIN_ORDER.getValue()
				: InvoiceTypeEnum.REPEAT_ORDER.getValue());
		// 下单门店
		createDto.setStoreId(orderSubscribe.getPlaceStoreId());
		// 用户id
		createDto.setUserId(orderSubscribe.getUserId());
		// 用车时长
		createDto.setTransportTime(orderSubscribe.getTransportTime());
		// 所属分子公司
		createDto.setSubcompanyId(dealerDetail.getSubcompanyId());

		return createDto;
	}

	public static InvoiceCreateDto toInvoiceCreateDto(NotifyBizDto notify, OrderSubscribeDto orderSubscribe,
			DealerDetailDto dealerDetail, BigDecimal amount, InvoiceTypeEnum invoiceType) {
		InvoiceCreateDto createDto = new InvoiceCreateDto();
		// 订单号
		createDto.setOrderCode(orderSubscribe.getOrderCode());
		// 下单时间
		createDto.setOrderTime(orderSubscribe.getOrderTime());
		// 支付时间/续订单生效的时
		createDto.setPayTime(notify.getPayTime());
		// 支付金额
		createDto.setAmount(amount);
		// 服务费
		createDto.setServiceFee(BigDecimal.ZERO);
		// 业务类型（1：订阅里程，2：专属里程）
		createDto.setBusinessType(InvoiceBusinessTypeEnum.SUBSCRIBE.getValue());
		// 发票类型（11：主订单，12：续订单，13：加购，14：超时，15：超程，16：质损,21：分期，22：全款）
		createDto.setType(invoiceType.getValue());
		// 下单门店
		createDto.setStoreId(orderSubscribe.getPlaceStoreId());
		// 用户id
		createDto.setUserId(orderSubscribe.getUserId());
		// 用车时长
		createDto.setTransportTime(orderSubscribe.getTransportTime());
		// 所属分子公司
		createDto.setSubcompanyId(dealerDetail.getSubcompanyId());

		return createDto;
	}
}
